var SolarTerms = angular.module('SolarTerms');

SolarTerms
//主控制器
    .controller("mainCtrl", function ($state, $timeout, $interval, $scope, $rootScope,config) {
        $rootScope.go_where = function (views, params) {
            $state.go(views, params);
        };
        $scope.AddFavorite=function(sURL, sTitle)
        {
            sURL= sURL?sURL:config.localUrl;
            sTitle= sTitle?sTitle:config.localTitle;

            try
            {
                window.external.addFavorite(sURL, sTitle);
            }
            catch (e)
            {
                try
                {
                    window.sidebar.addPanel(sTitle, sURL, "");
                }
                catch (e)
                {
                    alert("加入收藏失败，请使用Ctrl+D进行添加");
                }
            }
        }
    })
    .controller("slideCtrl", function ($scope) {
        $scope.$on('$viewContentLoaded', function () {
            $(document).ready(function () {
                $('.box_skitter_large').skitter({
                    theme: 'clean',
                    numbers_align: 'center',
                    progressbar: true,
                    dots: true,
                    preview: true,
                    velocity: 20,
                    auto_play: false,
                    interval: 10000
                });
            });
        })
    })
    .controller('homeCtrl', function (config,$http,$state, $rootScope, $scope,$timeout) {
        $rootScope.navShow = 'index.home';
        $scope.$on("$viewContentLoaded", function () {
            $scope.drawPicClick();
        });
        $scope.yundan = '';//用户输入的运单号
        $scope.code_input = '';//用户输入的

        //加载验证码
        $scope.drawPicClick = function () {
            $scope.code = drawPic('code_canvas1', 3).toUpperCase();
        };
        $scope.go_ser = function () {
            if ($scope.yundan.length == 0) {
                alert('请输入运单号');
                return false;
            }
            if ($scope.code_input.toUpperCase() != $scope.code) {
                alert('请输入正确验证码');
                return false;
            }
            $state.go('index.search', {view: 'Waybill@index.search', waybill: $scope.yundan})
        };//跳转至搜索页

        var _config={
            pageNo:1,
            pageSize:5
        };
        $http({method:'get',url:config.appCode+config.getNews,params:_config}).success(function(res){
            console.log(res);
            $scope.paginationData = res.data;
            $timeout(function(){
                $("#newslist").newsList({actName:'li'});
            },1000)

        });
        $scope.$on("$viewContentLoaded",function(){

        })
    })
    .controller('searchCtrl', function ($stateParams, $scope, $rootScope) {
        $scope.view = $stateParams.view;
        $scope.$on('$viewContentLoaded', function () {
            $rootScope.navShow = 'index.search';
        })
    })
    .controller('WaybillCtrl', function ($stateParams, config, $state, $timeout, $interval, $scope, $rootScope, $http) {
        //数据
        $scope.code_input = '';//用户输入的
        $scope.biilinput = '';
        $scope.codeIstrue = false;
        //加载验证码
        $scope.drawPicClick = function () {
            $scope.code = drawPic('code_canvas');
            console.log($scope.code);
        };
        $scope.$on('$viewContentLoaded', function () {
            $scope.code = drawPic('code_canvas');
            $(".ser_input").focus();
            if ($stateParams.waybill) {
                $scope.biilinput = $stateParams.waybill;
                $http({
                    url: config.billSearch + $scope.biilinput,
                    method: 'GET'
                }).success(function (res) {
                    res = res.data;
                    if (res.length > 0) {
                        //    整理运单
                        $scope.myNeedArr = {};
                        for (var i = 0; i < res.length; i++) {
                            var str1 = res[i].createDate.slice(0, 10);
                            var str2 = res[i].createDate.slice(12, 19);
                            if (!$scope.myNeedArr[str1]) {//是不是添加过日期了
                                $scope.myNeedArr[str1] = [];
                                $scope.myNeedArr[str1].push({
                                    info: res[i].info,
                                    date: str2
                                });
                            } else {
                                $scope.myNeedArr[str1].push({
                                    info: res[i].info,
                                    date: str2
                                });
                            }

                        }
                    } else {
                        alert('未查到相关信息')
                    }

                })
            }
        });
        //输入验证码
        $scope.code_keyup = function () {
            if ($scope.code.toUpperCase() == $scope.code_input.toUpperCase()) {
                $scope.codeIstrue = true;
                return true;
            } else {
                $scope.codeIstrue = false;
                $scope.drawPicClick();
            }
            return false;
        };
        //清空物流
        $scope.clear_bill = function () {
            $scope.biilinput = '';//用户输入的
        };
        //201612080002
        //$http({
        //    url:config.billSearch+201612080002,
        //    method:'GET'
        //}).success(function(res){
        //    console.log(res);
        //    if(res.length>0){
        //        //    整理运单
        //        $scope.myNeedArr={};
        //        for(var i=0;i<res.length;i++){
        //            var str1=res[i].date.slice(0,10);
        //            console.log(str1);
        //            var str2=res[i].date.slice(12,19);
        //            if(!$scope.myNeedArr[str1]){//是不是添加过日期了
        //                $scope.myNeedArr[str1]=[];
        //                $scope.myNeedArr[str1].push({
        //                    info:res[i].info,
        //                    date:str2
        //                });
        //            }else{
        //                $scope.myNeedArr[str1].push({
        //                    info:res[i].info,
        //                    date:str2
        //                });
        //            }
        //
        //        }
        //    }else{
        //
        //    }
        //
        //})
        //点击搜索
        $scope.clickSearch = function () {
            if ($scope.biilinput.length == 0) {
                alert('您还没有输入单号哦');
                return false;
            }
            if (!config.regNum12.test($scope.biilinput)) {
                alert('您输入单号有问题哦');
                return false;
            }
            if (!$scope.code_keyup()) {
                alert('您输入的验证码有误');
                return false;
            }

            //201612080002
            $http({
                url: config.appCode+config.billSearch + $scope.biilinput,
                method: 'GET'
            }).success(function (res) {
                res = res.data;
                if (res.length > 0) {
                    //    整理运单
                    $scope.myNeedArr = {};
                    for (var i = 0; i < res.length; i++) {
                        var str1 = res[i].createDate.slice(0, 10);
                        console.log(str1);
                        var str2 = res[i].createDate.slice(12, 19);
                        if (!$scope.myNeedArr[str1]) {//是不是添加过日期了
                            $scope.myNeedArr[str1] = [];
                            $scope.myNeedArr[str1].push({
                                info: res[i].info,
                                date: str2
                            });
                        } else {
                            $scope.myNeedArr[str1].push({
                                info: res[i].info,
                                date: str2
                            });
                        }

                    }
                } else {
                    alert('未查到相关信息')

                }

            })
        }
    })
    .controller('partnersCtrl', function ($stateParams, config, $state, $timeout, $interval, $scope, $rootScope, $http) {
        $rootScope.navShow = 'index.partners';
        $scope.showMyMap=function() {
            $rootScope.dialogIsShow = true;
            //地图初始化时，在地图上添加一个marker标记,鼠标点击marker可弹出自定义的信息窗体
            var map = new AMap.Map("map2", {
                resizeEnable: true,
                center: [121.03802,31.341998],
                zoom: 25
            });
            AMap.plugin(['AMap.ToolBar','AMap.Scale','AMap.OverView','AMap.MapType','AMap.Geolocation'],
                function(){
                    map.addControl(new AMap.ToolBar());

                    map.addControl(new AMap.Scale());

                    map.addControl(new AMap.OverView({isOpen:true}));
                    map.addControl(new AMap.MapType());
                    map.addControl(new AMap.Geolocation());


                });
            addMarker();
            //添加marker标记
            // 实例化点标记
            function addMarker() {
                map.clearMap();
                marker = new AMap.Marker({
                    map: map,
                    icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
                    position: [121.03802,31.341998]
                });
                marker.setMap(map);
                //鼠标点击marker弹出自定义的信息窗体
                AMap.event.addListener(marker, 'click', function() {
                    infoWindow.open(map, marker.getPosition());
                });
            }
            openInfo();
            //在指定位置打开信息窗体
            function openInfo() {
                //构建信息窗体中显示的内容
                var info = [];
                info.push("<div><div><img style=\"float:left;height:30px;\" src=\" images/logoSC_02.png \"/></div> ");
                info.push("<div style=\"padding:0px 0px 0px 4px;\"><b>臻程物流</b>");
                info.push("电话 : 0512-55252088   邮编 : 215332");
                info.push("地址 :苏州市昆山市丰夏路99号源丰物流园</div></div>");
                infoWindow = new AMap.InfoWindow({
                    content: info.join("<br/>")  //使用默认信息窗体框样式，显示信息内容
                });
                infoWindow.open(map, map.getCenter());
            }
        }
        $scope.closeMyMap=function(){
            $rootScope.dialogIsShow=false;

        }
    })

    .controller('newsListCtrl', function (config,$http,$scope,paginateSer,$rootScope) {
        $rootScope.navShow = 'index.newsList';

        $scope.paginateSer = paginateSer;

        var _config={
            pageNo:1,
            pageSize:5
        };
        $http({method:'get',url:config.appCode+config.getNews,params:_config}).success(function(res){
            console.log(res);
            $scope.paginationData = res.data;
            paginateSer.paginate.init(res.total, _config.pageSize, _config.pageNo);
        });
        paginateSer.paginate.promise = function (_self) {
            $http({method:'get',url:config.appCode+config.getNews,params:{
                pageNo:this.pageNum,
                pageSize:this.pageSize
            }}).success(function (res) {
                $scope.paginationData = res.data;
                _self.init(res.total, _self.pageSize, _self.pageNum);
            }).error(function (data) {
                alert(JSON.stringify(data));
            });
        };
    })
    .controller("newsInfoCtrl",function(config,$stateParams,$http,$rootScope,$state,$scope){
        $rootScope.navShow = 'index.newsList';

        console.log($stateParams);
        var _config={
            id:$stateParams.id
        };
        if(!$stateParams.id){go_where('index.newsList',{view:'index.newsList'})}
        $http({method:'get',url:config.appCode+config.getNewsById,params:_config}).success(function(res){
            console.log(res);
            $scope.newsDetail=res.data;
        });
    })
    .controller("aboutUsCtrl",function($rootScope,$scope){
    $rootScope.navShow='index.aboutUs';

})
;
